package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	. "web_api_gin/controller"
	"web_api_gin/core"
	"web_api_gin/dao"
	"web_api_gin/dto"
	"web_api_gin/entity"
	"web_api_gin/service"
)

var SysMenuApi = SysMenuController{}

type SysMenuController struct {
}

//菜单首页列表
func (c *SysMenuController) Index(ctx *gin.Context) {
	var searchParams dto.SysMenuGetListRequest
	if err := ctx.ShouldBind(&searchParams); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}

	//菜单分组列表
	groupList, _, err := dao.SysMenuGroup.GetMenuGroupList(ctx, &dto.SysMenuGroupGetListRequest{})
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, err.Error())
		return
	}
	//菜单列表
	menuList, err := dao.SysMenu.GetMenuList(ctx, &searchParams)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusExpectationFailed, err.Error())
		return
	}
	var menuTreeList []*dto.SysMenusBig
	//转换为树结构数据
	if searchParams.Name == "" {
		menuTreeOriginList := ([]*dto.SysMenusBig)(nil)
		for _, menu := range menuList {
			menuTreeOriginList = append(menuTreeOriginList, &dto.SysMenusBig{
				SysMenu: *menu,
			})
		}
		menuTreeList = service.SysMenu.BuildTree(menuTreeOriginList)
	} else {
		for _, menu := range menuList {
			menuTreeList = append(menuTreeList, &dto.SysMenusBig{
				SysMenu: *menu,
			})
		}
	}
	//权限列表
	permList, err := dao.SysPermission.GetAll(ctx)
	permTreeOriginList := ([]*dto.SysPermissionTree)(nil)
	for _, permission := range permList {
		permTreeOriginList = append(permTreeOriginList, &dto.SysPermissionTree{
			SysPermission: *permission,
		})
	}
	permTreeList := service.SysPermission.BuildTree(permTreeOriginList)
	//显示输出
	Success(ctx, gin.H{
		"groupList": groupList,
		"menuList":  menuTreeList,
		"permList":  permTreeList,
	})
}

// 菜单保存
func (c *SysMenuController) Save(ctx *gin.Context) {
	var form *entity.SysMenu
	if err := ctx.ShouldBind(&form); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	//设置表单数据，方便保存日志
	ctx.Set("formData", form)
	rows, err := dao.SysMenu.SaveMenu(ctx, form)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, err.Error())
		return
	}
	Success(ctx, gin.H{
		"rows": rows,
	})
}

//菜单删除
func (c *SysMenuController) Destroy(ctx *gin.Context) {
	var form *dto.SysMenuFormDestroy
	if err := ctx.ShouldBind(&form); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	//设置表单数据，方便保存日志
	ctx.Set("formData", form)
	rows, err := dao.SysMenu.DelMenu(ctx, form.MenuIds)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, err.Error())
		return
	}
	Success(ctx, gin.H{
		"rows": rows,
	})
}

// 设置排序
func (c *SysMenuController) SetOrder(ctx *gin.Context) {
	var form *dto.SysMenuSetOrderRequest
	if err := ctx.ShouldBind(&form); err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, core.ValidatorTranslateError(err))
		return
	}
	//设置表单数据，方便保存日志
	ctx.Set("formData", form)
	err := dao.SysMenu.SetOrder(ctx, form)
	if err != nil {
		ErrorWithMessage(ctx, http.StatusBadRequest, err.Error())
		return
	}
	Success(ctx, gin.H{
		"form": form,
	})
}
